package com.webinsky.article.persist.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.webinsky.article.domain.MisArticleFilter;
import com.webinsky.common.proj.DelFlag;
import com.webinsky.common.utils.db.BasicSqlProvider;
import com.webinsky.common.utils.db.PageHelper;
import com.webinsky.common.utils.db.SQLBuilder;

public class ArticleMapperProvider extends BasicSqlProvider {

	private String baseSQL = " select `a`.`id`,"
							   + "`a`.`article_no`,"
							   + "`a`.`title`,"
							   + "`a`.`file_name`,"
							   + "`a`.`abstract`,"
							   + "`a`.`language`,"
							   + "`a`.`site_id`,"
							   + "`a`.`remark`,"
							   + "`u`.`name`,"
							   + "`a`.`update_user_id`,"
							   + "`p`.`name` as update_user_name,"
							   + "`a`.`user_id`,"
							   + "`a`.`can_modify`,"
							   + "`a`.`update_at`"
							   + " from `article` a left join user u on a.user_id = u.id and u.del = 0 "
							   + " left join user p on `a`.`update_user_id` = p.id and p.del = 0 ";


	
	private String baseCountSQL = " select count(*) from `article` a left join user u on a.user_id = u.id and u.del = 0  "
							   + " left join user p on `a`.`update_user_id` = p.id and p.del = 0 ";
	
	private String[] cols = new String[]{"id","title","file_name", "abstract", "article_no", "can_modify", "site_id", "remark", "del", "user_id", "update_user_id", "update_user_name", "create_at", "update_at", "language"};
	
	private String SELECT_BYNOS = "select id, site_id, file_name, article_no, title, abstract, remark, del, user_id, update_user_id, update_user_name, create_at, update_at, language, can_modify from `article`";
	
	
	public ArticleMapperProvider() {
		super("article");
	}
	
	private String buildSelectByFilter(String strBaseSQL, Map<String, Object> param){
		MisArticleFilter filter = (MisArticleFilter)param.get("filter");
		String sqlBuilder = strBaseSQL;
		List<String> conditions = new ArrayList<>();
		conditions.add("`a`.`del` = 0");
		if ( filter != null ){
			if ( filter.getArticleNo() != null && !filter.getArticleNo().trim().equals("") ){
				conditions.add("UPPER(`a`.`article_no`) like '%" + filter.getArticleNo().toUpperCase() + "%'");
			}
			if ( filter.getTitle() != null && !filter.getTitle().trim().equals("") ){
				conditions.add("`a`.`title` like '%" + filter.getTitle() + "%'");
			}
			if ( filter.getUpdator() != null && !filter.getUpdator().trim().equals("")){
				conditions.add("`p`.`name` like '%" + filter.getUpdator() + "%'");
			}
			if ( filter.getSiteId() > 0 ){
				conditions.add("`a`.`site_id` = " + filter.getSiteId() );
			}
			if ( filter.getLang() != null ){
				conditions.add("`a`.`language` = '" + filter.getLang().getLang() + "'" );
			}
			if ( filter.getCanModify() >= 0 && filter.getCanModify() <= 1 ){
				conditions.add("`a`.`can_modify` = " + filter.getCanModify());
			}
		}
		if ( !conditions.isEmpty() ){
			sqlBuilder = sqlBuilder + " where " + String.join(" and ", conditions);
		}
		// 查看Order
		String strOrder = "";
		if ( filter.getPagnation() != null && filter.getPagnation().getOrders() != null ){
			List<String> strOrders = new ArrayList<>();
			filter.getPagnation().getOrders().forEach( order -> {
				if ( order.getFieldName() != null && order.getIsAscOrDes() != null ){
					String ascOrDesc = order.getIsAscOrDes() ? "Asc" : "Desc";
					strOrders.add(order.getFieldName() + " " + ascOrDesc);
				}
			});
			if ( !strOrders.isEmpty() ) {
				strOrder = " Order By " + String.join(",", strOrders);
			}
		}
		
		return sqlBuilder + strOrder;
	}
	
	@SuppressWarnings("unchecked")
	public String selectByNos(Map<String, Object> params){
		Integer siteId = (Integer)params.get("siteId");
		String lang = (String)params.get("lang");
		List<String> nos = (List<String>)params.get("articleNos");
		String conditions = " where del = " + DelFlag.UNDel.getFlag() + " and site_id = " + siteId + " and language = '" + lang + "'";
		if ( nos != null && !nos.isEmpty() ){
			conditions = conditions + " and article_no in('" + String.join("','", nos) + "')";
		}
		return SELECT_BYNOS + conditions;
	}
	
	@SuppressWarnings("unchecked")
	public String selectByIds(Map<String, Object> param){
		List<Integer> ids = (List<Integer>)param.get("list");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = " + DelFlag.UNDel.getFlag());
		if ( ids != null && !ids.isEmpty() ){
			List<String> strIds = ids.stream().map(e -> e.toString()).collect(Collectors.toList());
			sqlBuilder.AND();
			sqlBuilder.WHERE(" id in (" + String.join(",", strIds) + ")");
		}
		return sqlBuilder.toString();
	}
	
	public String selectByFilter(Map<String, Object> param){
		MisArticleFilter filter = (MisArticleFilter)param.get("filter");
		return PageHelper.startPage(buildSelectByFilter(baseSQL, param), filter.getPagnation().getPageStart(), filter.getPagnation().getPageSize());
	}
	
	public String selectCountByFilter(Map<String, Object> param){
		return buildSelectByFilter(baseCountSQL, param);
	}
	
}
